package com.resou.reader.mine.mybooklist;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.MyBookListBean;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.ResultList;
import com.resou.reader.api.service.StoreService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.UserUtil;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.observers.BlockingBaseObserver;

public class MyBooklistPresenter extends ResouBasePresenter<IMyBooklistContract.IMyBooklistView> implements IMyBooklistContract.IMyBookListPresenter {

    StoreService storeService;

    public MyBooklistPresenter(IMyBooklistContract.IMyBooklistView view) {
        super(view);
        storeService = ApiImp.getInstance().getService(StoreService.class);
    }

    /**
     * 获取我的书单
     *
     * @param page
     */
    @Override
    public void quearBookList(int page) {
        mView.showProgress();
        storeService.myBookList(UserUtil.getToken(), page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result<MyBookListBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<MyBookListBean> userBookListBeanResult) {
                        localNum = (page + 1) * pageSize;
                        int totalNum = userBookListBeanResult.getData().getTotalPage() * pageSize;
                        if (localNum < totalNum) {
                            mView.showBookList(userBookListBeanResult.getData().getUserBookList());
                            mView.getSmartRefreshLayout().finishLoadMore(true);
                        } else {
                            if (totalNum == 0) {
                                mView.showEmpty();
                            }
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            mView.showBookList(userBookListBeanResult.getData().getUserBookList());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.getSmartRefreshLayout().finishRefresh();
                        mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    @Override
    public void deleteBookList(String bookId) {
        storeService.deteleCollction(UserUtil.getToken(), bookId).subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers()).subscribe(new Observer<Result>() {
            @Override
            public void onSubscribe(Disposable d) {
                addCompositeDisposable(d);
            }

            @Override
            public void onNext(Result result) {
                if (result.getCode() == 0) {
                    mView.cancellCollection();
                } else {
                    mView.showInfo("取消收藏失败");
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.showInfo(e.getMessage());
            }

            @Override
            public void onComplete() {
            }
        });
    }


}
